package com.pingan.myBatis.crypt.handler;

import com.pingan.myBatis.crypt.utils.IsNotCryptUtil;

import java.util.List;
import java.util.Map;

/**
 * @AUTHOR:LIUCHAO;
 * @DATE: 2020/11/30 16:34
 */
public class ListHandler {


    /**
     * list 加密
     *
     * @param list
     * @param bo
     * @return
     * @throws Exception
     */
    public static List listEncrypt(List list, Boolean bo) throws Exception {
        for (int i = 0; i < list.size(); i++) {
            Object listValue = list.get(i);
            // 判断不需要解析的类型
            if (IsNotCryptUtil.isNotCrypt(listValue) || listValue instanceof Map) {
                break;
            }
            if (listValue instanceof String && bo) {
                list.set(i, StringHandler.stringEncrypt((String) listValue));
                continue;
            }
            BeanHandler.beanEncrypt(listValue);
        }

        return list;
    }

    /**
     * list 解密
     *
     * @param list
     * @param bo
     * @return
     * @throws Exception
     */
    public static List listDecrypt(List list, Boolean bo) throws Exception {
        for (int i = 0; i < list.size(); i++) {
            Object listValue = list.get(i);
            // 判断不需要解析的类型 获得
            if (IsNotCryptUtil.isNotCrypt(listValue) || listValue instanceof Map) {
                break;
            }
            if (listValue instanceof String && bo) {
                list.set(i, StringHandler.stringDecrypt((String) listValue));
                continue;
            }
            BeanHandler.beanDecrypt(listValue);
        }

        return list;
    }
}
